home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / DIOFNC11.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  8KB  |  253 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   DIOFNC11.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.01.00        February 1991
  6.  *  Language    :   Microsoft C     Version 5.1
  7.  *  Purpose     :   Intel 8255 Compatible Digital IO Functions
  8.  *              :   Port Array Functions
  9.  *  ----------------------------------------------------------------------
  10.  *  WARNING : I do not have a multi-port board on which to test
  11.  *  these functions.  The single-port functions work correctly, and I
  12.  *  wrote these multi-port functions as an extension to those
  13.  *  single-port functions for those of you with multi-port boards.
  14.  *  These functions here 'seem' correct, but are not truly debugged.
  15.  *  ----------------------------------------------------------------------
  16.  *  Revision History:
  17.  *  022891 BVM  :   Creation
  18.  *  ----------------------------------------------------------------------
  19.  */
  20.  
  21. #define     DIOFNC11_C_DEFINED  1
  22. #include    "malloc.h"      /* malloc() free()  */
  23. #include    "stdio.h"       /* NULL definition  */
  24. #include    "DIOLIB.H"
  25. #undef      DIOFNC11_C_DEFINED
  26.  
  27. static DIODAT  *dadat = (DIODAT *)NULL; /* array : base pointer     */
  28. static short    dasiz = 0;              /* array : element count    */
  29. static short    mxbit = 0;              /* array : maximum bit      */
  30.  
  31. short dio_pa_aloc   (short);
  32. void  dio_pa_free   (void);
  33. short dio_pa_setadr (short, short);
  34. short dio_pa_bitput (short, short);
  35. short dio_pa_bitget (short, short *);
  36. short dio_pa_getptr (short, DIODAT **);
  37. short dio_pa_config (short, short, short, short, short);
  38.  
  39.  
  40. /*- DIO : Allocate Data Array Space ----------**
  41.  *  Allocate memory for the DIODAT data array.
  42.  *  Passed:
  43.  *      short   :   number of DIODAT elements to be in array
  44.  *  Returns:
  45.  *      short   :   DIO_ST_OK = OK
  46.  *              :   DIO_ST_NM = No memory available
  47.  *              :   DIO_ST_MA = Existing memory not yet free
  48.  */
  49. short dio_pa_aloc (short danum)
  50.     {
  51.     /*  Memory already in use.
  52.      */
  53.     if ( dadat != (DIODAT *)NULL )
  54.         return (DIO_ST_MA);
  55.  
  56.     /*  Allocate memory.
  57.      *  Check for failure.
  58.      */
  59.     dadat = malloc ( danum * sizeof(DIODAT) );
  60.     if ( dadat == (DIODAT *)NULL )
  61.         return (DIO_ST_NM);
  62.  
  63.     /*  Save count of array elements.
  64.      *  Default data even though don't know addresses yet.
  65.      *  Calculate maximum bit for this array size.
  66.      */
  67.     dasiz = danum;
  68.     for ( danum = 0; danum < dasiz; danum++ ){
  69.         dio_init ( &dadat[danum], 0);
  70.         }
  71.  
  72.     mxbit = danum * 24;
  73.  
  74.     return (DIO_ST_OK);
  75.     }
  76.  
  77.  
  78. /*- DIO : Free Data Array Space --------------**
  79.  *  Allocate memory for the DIODAT data array.
  80.  *  It does no harm to call this function if the
  81.  *  memory has not already been allocated.
  82.  *  Passed:
  83.  *      short   :   number of DIODAT elements to be in array
  84.  *  Returns:
  85.  *      Nothing
  86.  */
  87. void dio_pa_free (void)
  88.     {
  89.     if ( dadat != (DIODAT *)NULL ){
  90.         dasiz = 0;          /* set count to zero    */
  91.         free ( dadat );     /* free array memory    */
  92.         }
  93.     }
  94.  
  95.  
  96. /*- DIO : Set Element Address ----------------**
  97.  *  Set the 8255 address of one of the array elements.
  98.  *  Passed:
  99.  *      short   :   DIODAT element affected (Base 0)
  100.  *      short   :   8255 base address
  101.  *  Returns:
  102.  *      short   :   DIO_ST_OK = OK
  103.  *              :   DIO_ST_NM = Memory not allocated
  104.  *              :   DIO_ST_BE = Bad element
  105.  */
  106. short dio_pa_setadr (short danum, short basadr)
  107.     {
  108.  
  109.     /*  Check for errors.
  110.      *      No memory.
  111.      *      Bad element number.
  112.      *  Otherwise assign address.
  113.      */
  114.     if ( dasiz == 0 )
  115.         return (DIO_ST_NM);
  116.     if ( (danum < 0) || (danum >= dasiz) )
  117.         return (DIO_ST_BE);
  118.  
  119.     dadat[danum].base = basadr;
  120.     dadat[danum].stat = DIO_ST_OK;
  121.     return (DIO_ST_OK);
  122.     }
  123.  
  124.  
  125. /*- DIO : Array Bit Put ----------------------**
  126.  *  Set/Clear one of the bits in the 8255 port array.
  127.  *  A state of !0 sets the bit and a state of 0 clears the bit.
  128.  *  The bit number should be from 0 -> mxbit-1 (Base 0).
  129.  *  Passed:
  130.  *      short   :   bit number
  131.  *      short   :   state : TRUE (!0) = SET, FALSE (0) = CLEAR
  132.  *  Returns:
  133.  *      short   :   DIO_ST_OK = OK
  134.  *              :   DIO_ST_NM = Memory not allocated
  135.  *              :   DIO_ST_BB = Bad bit number
  136.  */
  137. short dio_pa_bitput (short bit, short state)
  138.     {
  139.     short   danum;
  140.  
  141.     if ( dasiz == 0 )
  142.         return (DIO_ST_NM);
  143.     if ( (bit < 0) || (bit >= mxbit) )
  144.         return (DIO_ST_BB);
  145.  
  146.     danum = ( bit / 24);    /* 8255 array element number    */
  147.     bit = bit % 24;         /* 8255 compensated bit number  */
  148.  
  149.     dio_bitput (&dadat[danum], bit, state);
  150.     dadat[danum].stat = DIO_ST_OK;
  151.     return (DIO_ST_OK);
  152.     }
  153.  
  154.  
  155.  
  156. /*- DIO : Array Bit Get ----------------------**
  157.  *  Read one of the bits in the 8255 port array.
  158.  *  A state of 1 indicates a set bit and a state of 0
  159.  *  indicates a clear bit.
  160.  *  The bit number should be from 0 -> mxbit-1 (Base 0).
  161.  *  Passed:
  162.  *      short   :   bit number
  163.  *      pointer :   short :   state : 1 = SET, 0 = CLEAR
  164.  *  Returns:
  165.  *      short   :   DIO_ST_OK = OK
  166.  *              :   DIO_ST_NM = Memory not allocated
  167.  *              :   DIO_ST_BB = Bad bit number
  168.  *      Loads state with 0 or 1.
  169.  */
  170. short dio_pa_bitget (short bit, short *state)
  171.     {
  172.     short   danum;
  173.  
  174.     if ( dasiz == 0 )
  175.         return (DIO_ST_NM);
  176.     if ( (bit < 0) || (bit >= mxbit) )
  177.         return (DIO_ST_BB);
  178.  
  179.     danum = ( bit / 24);    /* 8255 array element number    */
  180.     bit = bit % 24;         /* 8255 compensated bit number  */
  181.  
  182.     dio_bitget (&dadat[danum], bit, state);
  183.     dadat[danum].stat = DIO_ST_OK;
  184.     return (DIO_ST_OK);
  185.     }
  186.  
  187.  
  188. /*- DIO : Get Array Address ------------------**
  189.  *  Return address of the DIODAT array.
  190.  *  Note that NULL is returned if memory not allocated.
  191.  *  Passed:
  192.  *      short   :   element number
  193.  *      pointer :   DIODAT *
  194.  *  Returns:
  195.  *      short   :   DIO_ST_OK = OK
  196.  *              :   DIO_ST_NM = Memory not allocated
  197.  *              :   DIO_ST_BE = Bad element number
  198.  */
  199. short dio_pa_getptr (short danum, DIODAT **diopp)
  200.     {
  201.     if ( dasiz == 0 )
  202.         return (DIO_ST_NM);
  203.     if ( (danum < 0) || (danum >= dasiz) )
  204.         return (DIO_ST_BE);
  205.     *diopp = &dadat[danum];
  206.     return (DIO_ST_OK);
  207.     }
  208.  
  209.  
  210. /*- DIO : Array Element Configure ------------**
  211.  *  Configure port direction for one of the 8255s in the array.
  212.  *  Passed:
  213.  *      short   :   element number
  214.  *      short   :   direction Port A
  215.  *      short   :   direction Port B
  216.  *      short   :   direction Port C Low
  217.  *      short   :   direction Port C High
  218.  *  Returns:
  219.  *      short   :   DIO_ST_OK = OK
  220.  *              :   DIO_ST_NM = Memory not allocated
  221.  *              :   DIO_ST_BE = Bad element number
  222.  */
  223. short dio_pa_config (short danum, short pa_dir, short pb_dir,
  224.                                   short cl_dir, short ch_dir)
  225.     {
  226.     if ( dasiz == 0 )
  227.         return (DIO_ST_NM);
  228.     if ( (danum < 0) || (danum >= dasiz) )
  229.         return (DIO_ST_BE);
  230.  
  231.     /*  Initially set mode to DIO_SET since we are
  232.      *  going to configure the 8255.
  233.      *  Then OR in the IO direction bits as appropriate.
  234.      */
  235.  
  236.     dadat[danum].mode = DIO_SET;
  237.  
  238.     if ( pa_dir )   dadat[danum].mode |= DIO_PA_IN;
  239.     if ( pb_dir )   dadat[danum].mode |= DIO_PB_IN;
  240.     if ( cl_dir )   dadat[danum].mode |= DIO_CL_IN;
  241.     if ( ch_dir )   dadat[danum].mode |= DIO_CH_IN;
  242.  
  243.     dio_bput ( dadat[danum].base + DIO_CNTRL, dadat[danum].mode );
  244.     dadat[danum].stat = DIO_ST_OK;
  245.     return (DIO_ST_OK);
  246.     }
  247.  
  248. /*-
  249.  *  ----------------------------------------------------------------------
  250.  *  END DIOFNC11.C Source File
  251.  *  ----------------------------------------------------------------------
  252.  */
  253.